<?php 

class Vs_FbLoginFbAuthModuleFrontController extends ModuleFrontController	
{

	const DEFAULT_GROUP = 2;
	
	public function init()
	{
		parent::init();
		file_put_contents(dirname(__FILE__).'/debug_log', 'EHO');
	}

	public function initContent()
	{
		file_put_contents(dirname(__FILE__).'/debug_log', print_r($_POST, true));
		// Ne mi trqbva i parenta
		parent::initContent();
		file_put_contents(dirname(__FILE__).'/debug_log', print_r($_POST, true));
		$fbData = array();
		if(Tools::isSubmit("submitFbLogin")) {
			$fbData['email']    = Tools::getValue('email');
			$fbData['name']     = Tools::getValue('name');
			$fbData['imageSrc'] = Tools::getValue('imageSrc');
			file_put_contents(dirname(__FILE__).'/debug_log', print_r($fbData, true));
			$customerId = Customer::customerExists($fbData['email'], true);
			if($customerId) {
				$customer = new Customer($customerId);
				
				if($customer->id_default_group != self::DEFAULT_GROUP) {
					$customer->id_default_group = self::DEFAULT_GROUP;	
					$customer->save();
				}
			}
			else {
				$customer = $this->_createAccount($fbData);
			}
			
			if($customer) {
				$this->_createSession($customer);
			}
			Tools::redirect(Tools::getValue('returnUrl'));
		}
		
		//echo "<pre>"; die(var_dump($fbData));
	}
	
	private function _createAccount($data)
	{
		//file_put_contents(dirname(__FILE__).'/debug_log', print_r($data, true));
		$nameParts = explode(' ', $data['name']);
		$customer = new Customer();
	
		$customer->email 	 = $data['email'];
		$customer->lastname  = isset($nameParts[1]) ? $nameParts[1] : '';
		$customer->firstname = isset($nameParts[0]) ? $nameParts[0] : '';
		$customer->passwd    = Tools::encrypt(Tools::passwdGen());
	
		$customer->id_default_group = self::DEFAULT_GROUP;
		$customer->add();
		
		/*
		 * If Add Customer Error
		 */
		if ($customer->id) {
			return $customer;
		}
		else {
			$this->logs[] = $this->l('Cannot create customer');
			return null;
		}
		
	}
	
	private function _createSession($customer)
	{
		self::$cookie->id_compare = isset(self::$cookie->id_compare) ? self::$cookie->id_compare: CompareProduct::getIdCompareByIdCustomer($customer->id);
		self::$cookie->id_customer = (int)($customer->id);
		self::$cookie->customer_lastname = $customer->lastname;
		self::$cookie->customer_firstname = $customer->firstname;
		self::$cookie->logged = 1;
		$customer->logged = 1;
		self::$cookie->is_guest = $customer->isGuest();
		self::$cookie->passwd = $customer->passwd;
		self::$cookie->email = $customer->email;
	
		// Add customer to the context
		self::$customer = $customer;
	
		if (Configuration::get('PS_CART_FOLLOWING') && (empty(self::$cookie->id_cart) || Cart::getNbProducts(self::$cookie->id_cart) == 0))
			self::$cookie->id_cart = (int)Cart::lastNoneOrderedCart(self::$customer->id);
	
		// Update cart address
		self::$cart->id = self::$cookie->id_cart;
		self::$cart->setDeliveryOption(null);
		self::$cart->id_address_delivery = Address::getFirstCustomerAddressId((int)($customer->id));
	
		self::$cart->id_address_invoice = Address::getFirstCustomerAddressId((int)($customer->id));
		self::$cart->secure_key = $customer->secure_key;
		self::$cart->update();
		self::$cart->autosetProductAddress();
	
		Hook::exec('actionAuthentication');
	
		// Login information have changed, so we check if the cart rules still apply
		CartRule::autoRemoveFromCart($this->context);
		CartRule::autoAddToCart($this->context);
		
		self::$smarty->assign('authentification_error', $this->errors);
	}
	
};

?>